<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Validator;
use Hash;
use Auth;
class PasswordRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    //自定义规则
    public function zidingyiguize(){
        Validator::extend('check_password',function ($attribute,$value,$parameters,$validator){
            //判断用户输入的密码是否为当前的登陆密码
            return Hash::check($value,Auth::user()->password);
        });
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        //调用自身定义的验证规则
        $this->zidingyiguize();

        return [
            'username'=>'required|between:1,20',
            'oldpassword'=>'required|check_password',
            'password'=>'nullable|between:6,18|confirmed',
            'password_confirmation'=>'nullable|between:6,18',
        ];
    }
     public function messages()
     {
         return [
           'oldpassword.check_password'=>'旧密码输入错误，请重新输入！',
           'oldpassword.required'=>'旧密码不能为空！',
         ];
     }
}
